package com.admin.socketmultiplayer.activity

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextWatcher
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.SaveListener
import com.admin.socketmultiplayer.R
import com.admin.socketmultiplayer.bean.MyUser
import com.admin.socketmultiplayer.config.DataUtils
import com.admin.socketmultiplayer.config.FixedValue
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/5/8.
 */
class RegisterActivity:BaseActivity(){
    var userName=""
    var password=""
    var eamil=""

    override fun bindLayout(): Int {
        return R.layout.activity_register
    }

    override fun initData(){
        iv_back.setImageResource(R.mipmap.return_normal)
        tv_title.text="注册"
    }

    override fun initEvent(){
        iv_back.setOnClickListener { finish() }

        btn_register.setOnClickListener {
            userName=edt_user_name.text.toString()
            if (userName==""){
                toast("请输入用户名！")
                return@setOnClickListener
            }

            var query=BmobQuery<BmobUser>()
            query.addWhereEqualTo("username",userName)
            query.findObjects(object :FindListener<BmobUser>(){
                override fun done(p0: MutableList<BmobUser>?, p1: BmobException?) {
                    if(p0==null||p0.size==0){
                        password=edt_password.text.toString()
                        if(password==""||password.length<6){
                            toast("请输入至少6位吗数密码！")
                        }else{
                            eamil=edt_email.text.toString()
                            if(eamil==""||DataUtils.isPhoneNumRight(eamil,FixedValue.EAMIL)){
                                toast("请输入正确的邮箱地址")
                                return
                            }else{
                                var query=BmobQuery<MyUser>()
                                query.addWhereEqualTo("email",eamil)
                                query.findObjects(object:FindListener<MyUser>(){
                                    override fun done(p0: MutableList<MyUser>?, p1: BmobException?) {
                                        if (p0!=null&&p0.size>0){
                                            toast("该邮箱已被其他用户占用")
                                        }else{
                                            var user=BmobUser()
                                            user.username=userName
                                            user.setPassword(password)
                                            user.email=eamil
                                            register(user)
                                        }
                                    }
                                })
                            }

                        }
                    }else{
                        toast("用户名已存在")
                    }
                }

            })

        }
    }

    fun register(user:BmobUser){
        user.signUp(object:SaveListener<BmobUser>(){
            override fun done(p0: BmobUser?, p1: BmobException?) {
                p1?:let { toast("注册成功")
                    finish()
                }
                p1?.let {
                    toast("注册失败"+p1.message.toString())
                }
            }
        })
    }

}